package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import com.tanhua.server.service.SmallVideosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/smallVideos")
public class SmallVideosController {

    @Autowired
    private SmallVideosService videosService;

    @Autowired
    private CommentsService commentsService;

    /**
     * 发布视频
     * 接口路径：POST
     * 请求参数：
     * videoThumbnail：封面图
     * videoFile：视频文件
     */
    @PostMapping
    public ResponseEntity saveVideos(MultipartFile videoThumbnail, MultipartFile videoFile) throws IOException {
        videosService.saveVideos(videoThumbnail, videoFile);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频列表
     */
    @GetMapping
    public ResponseEntity queryVideoList(@RequestParam(defaultValue = "1") Integer page,
                                         @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult result = videosService.queryVideoList(page, pagesize);
        return ResponseEntity.ok(result);
    }

    /**
     * 视频点赞
     * 请求方式和路径：POST/smallVideos/:id/like
     * 请求参数：路径参数：id【视频id】
     * 响应数据：无
     */
    @PostMapping("/{id}/like")
    public ResponseEntity like(@PathVariable("id") String VideoId) {
        //1、接收参数，掉用service执行业务
        Integer likeCount = commentsService.likeVideo(VideoId);
        //2、响应数据
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 视频点赞-取消
     * 请求方式和路径：POST/smallVideos/:id/dislike
     * 请求参数：路径参数：id【视频id】
     * 响应数据：无
     */
    @PostMapping("/{id}/dislike")
    public ResponseEntity dislike(@PathVariable("id") String VideoId) {
        //1、接收参数，掉用service执行业务
        Integer likeCount = commentsService.dislikeVideo(VideoId);
        //2、响应数据
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 评论列表
     * 请求方式和路径：GET/smallVideos/:id/comments
     * 请求参数：路径参数：id【视频id】
     * 响应数据：PageResult
     */
    @GetMapping("/{id}/comments")
    public ResponseEntity comments(@RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "10") Integer pagesize,
                                   @PathVariable("id") String VideoId
    ) {
        //1、接收参数，调用service执行业务
        PageResult pr = commentsService.videoComments(page, pagesize, VideoId);
        //2、响应数据
        return ResponseEntity.ok(pr);
    }

    /**
     * 评论发布
     * 请求方式和路径：POST/smallVideos/:id/comments
     * 请求参数：路径参数：id【视频id】 、Body comment
     * 响应数据：无
     */
    @PostMapping("/{id}/comments")
    public ResponseEntity comments(@PathVariable("id") String VideoId,
                                   @RequestBody Map map
    ) {
        String comment = map.get("comment").toString();
        commentsService.publishComments(VideoId, comment);
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注
     * 请求方式和路径：POST/smallVideos/:uid/userFocus
     * 请求参数:路径参数：uid
     * 响应数据：无
     */
    @PostMapping("/{uid}/userFocus")
    public ResponseEntity userFocus(@PathVariable("uid") Long publishUserId){
        //1、接收参数,调用service执行业务
        commentsService.userFocus(publishUserId);
        //2、响应数据
        return ResponseEntity.ok(null);
    }

    /**
     * 视频用户关注--取消
     * 请求方式和路径：POST/smallVideos/:uid/userFocus
     * 请求参数:路径参数：uid
     * 响应数据：无
     */
    @PostMapping("/{uid}/userUnFocus")
    public ResponseEntity userUnFocus(@PathVariable("uid") Long publishUserId){
        //1、接收参数,调用service执行业务
        commentsService.userUnFocus(publishUserId);
        //2、响应数据
        return ResponseEntity.ok(null);
    }

    /**
     * 视频：评论点赞
     * 请求方式和路径：POST/smallVideos/comments/:id/like
     * 请求参数：路径参数：id 【评论id】
     * 响应数据：无
     */
    @PostMapping("/comments/{id}/like")
    public ResponseEntity likeContent(@PathVariable("id") String contentId){
        //1、接收参数，调用service执行业务
        Integer likeCount = commentsService.likeContent(contentId);
        //2、响应数据
        return ResponseEntity.ok(likeCount);
    }

    /**
     * 视频：评论点赞-取消
     * 请求方式和路径：POST/smallVideos/comments/:id/dislike
     * 请求参数：路径参数：id 【评论id】
     * 响应数据：无
     */
    @PostMapping("/comments/{id}/dislike")
    public ResponseEntity dislikeContent(@PathVariable("id") String contentId){
        //1、接收参数，调用service执行业务
        Integer likeCount = commentsService.dislikeContent(contentId);
        //2、响应数据
        return ResponseEntity.ok(likeCount);
    }
}